using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Linq;
using System.Linq;


using Soussous.Business;
using Soussous.DAO;
using Soussous.DAO.Connection;

namespace Soussous.DAO.Impl
{
    class FriendDAO : AbstractDAO<Friend>
    {
        private static FriendDAO INSTANCE = new FriendDAO();

        public static FriendDAO getInstance()
        {
            return INSTANCE;
        }

        private FriendDAO()
        {
        }
		
		public Table<Friend> getTable(){
			
			return FriendDAO.getInstance().getAll();
		
		}
        
        public List<Friend> getMyFriend(int idUserMe) 
        {
            var FriendList = from e in getTable()
							 where e.User_from == idUserMe	
                             select e;
            
            
            return FriendList.ToList();
        }
		
		public Friend isMyFriend(int idUserMe,int idUserFr) {
            
            var userList = from e in getTable()
                            where e.User_from == idUserMe && e.User_to == idUserFr
                            select e;

            return userList.Count() > 0 ? userList.First() : null;
        }

        public void deleteFriend(int idUserMe, int idUserFr)
        {
            Table<Friend> friend = getTable();
            var result = (from fr in friend
                          where fr.User_from == idUserMe && fr.User_to == idUserFr 
                          select fr);
            friend.DeleteOnSubmit(result.Single());
            friend.Context.SubmitChanges();
        }

     }
}